package com.example.whetherapp;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		loadWeatherReport("http://weather.yahooapis.com/forecastrss?w=2295414&u=c");
	}

	private void loadWeatherReport(String urlString) {
		// TODO Auto-generated method stub
		StringBuilder builder=new StringBuilder();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

		StrictMode.setThreadPolicy(policy); 
		URL url;
		try {
			url = new URL(urlString);
		
			XmlPullParserFactory 	factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp=factory.newPullParser();

			xpp.setInput(getInputStream(url), "UTF_8");

			int eventType=xpp.getEventType();
			while(eventType!=XmlPullParser.END_DOCUMENT){
			  // Looking for a start tag
			  if(eventType==XmlPullParser.START_TAG){
			    //We look for "title" tag in XML response
			    if(xpp.getName().equalsIgnoreCase("title") ){
			      //Once we found the "title" tag, add the text it contains to our builder
			      builder.append(xpp.nextText()+"\n");
			      
			    }
			    else if(xpp.getName().equalsIgnoreCase("description")){
				      //Once we found the "title" tag, add the text it contains to our builder
			    	String tempString = xpp.nextText();
			    	String[] cdata = tempString.split("<br />");
			    	if(cdata.length > 1)
				      builder.append(cdata[1] + "\n" + cdata[2] + " n" + cdata[3] + "n" + cdata[4]+ "\n");
				      
				}
			  }

			  eventType=xpp.next();
			}
			String report = builder.toString().replaceAll("Yahoo!", "");
			TextView weatherTextView = (TextView) findViewById(R.id.w_report);
			weatherTextView.setText(report);
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public InputStream getInputStream(URL url) {
		  try {
		    return url.openConnection().getInputStream();
		  } catch (IOException e) {
		    return null;
		  }
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
